package com.byapp.fast.modules.match.controller;

import com.byapp.fast.common.utils.ResultUtils;
import com.byapp.fast.common.vo.ResultVO;
import com.byapp.fast.modules.match.dto.MatchBetDTO;
import com.byapp.fast.modules.match.entity.MatchExtEntity;
import com.byapp.fast.modules.match.entity.MatchRefEntity;
import com.byapp.fast.modules.match.service.MatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("/match")
@Api(value = "赛事模块接口", tags = {"赛事模块接口"})
@Slf4j
public class MatchController {

    @Autowired
    private MatchService matchService;

    @GetMapping("/time")
    @ApiOperation(value = "日期赛事分组数据", notes = "获取日期列表")
    public ResultVO dateList() {
        return ResultUtils.success(matchService.queryMatchDateGroup());
    }

    @GetMapping("/time/{matchTime}")
    @ApiOperation(value = "日期赛事列表数据", notes = "按照赛事日期获取赛事列表")
    public ResultVO dateMatchList(@PathVariable("matchTime") String matchTime) {
        return ResultUtils.success(matchService.queryMatchDateList(matchTime));
    }

    @GetMapping("/league")
    @ApiOperation(value = "联赛赛事分组数据", notes = "获取联赛列表")
    public ResultVO leagueList() {
        return ResultUtils.success(matchService.queryMatchLeagueGroup());
    }

    @GetMapping("/league/{leagueId}")
    @ApiOperation(value = "联赛赛事列表数据", notes = "按照联赛ID获取赛事列表")
    public ResultVO leagueMatchList(@PathVariable("leagueId") String leagueId) {
        return ResultUtils.success(matchService.queryMatchLeagueList(leagueId));
    }

    @GetMapping("/odd/{matchId}")
    @ApiOperation(value = "赛事玩法赔率/盘口详情数据", notes = "获取赛事玩法赔率列表")
    public ResultVO matchOddsList(@PathVariable("matchId") String matchId) {
        StopWatch stopWatch = new StopWatch("matchOddsList");

        // 查询赛事封盘状态
        stopWatch.start("查询赛事封盘状态");
        MatchExtEntity matchExtEntity = matchService.queryMatchExt(matchId);
        Integer betStatus = matchExtEntity.getBetStatus();
        stopWatch.stop();

        // 查询赛事赔率
        stopWatch.start("查询赛事赔率");
        List<MatchBetDTO> matchBetDTOS = matchService.queryMatchOddsList(matchId);
        stopWatch.stop();

        // 查询赛事信息
        stopWatch.start("查询赛事信息");
        MatchRefEntity matchRefEntity = matchService.queryById(matchId);
        matchRefEntity.setMatchBetDTOList(matchBetDTOS);
        matchRefEntity.setBetStatus(betStatus);
        stopWatch.stop();

        log.info("【执行日志】{}", stopWatch.prettyPrint());
        return ResultUtils.success(matchRefEntity);
    }

}
